package com.gavin.lingxi.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;


import com.gavin.lingxi.common.utils.DeviceUtils;
import com.gavin.lingxi.rtc.R;
import com.puppy.bingo.tools.ToastUtils;




import org.mym.plog.PLog;


import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 * 所有Activity的基类
 */
public abstract class AppBaseActivity extends AppCompatActivity {

    private static final String TAG = "AppBaseActivity";

    private Unbinder mUnBinder;
    private BroadcastReceiver netwokReceive;
    private ConnectivityManager.NetworkCallback networkCallback;
    public AppBaseActivity() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        overridePendingTransition(R.anim.app_comm_slide_in_from_right, R.anim.app_comm_slide_out_to_left);
        super.onCreate(savedInstanceState);
        if(getLayoutId() > 0){
            setContentView(getLayoutId());
        }
        if(needButterKnife()){
            mUnBinder = ButterKnife.bind(this, this);
        }

    }

    protected void onNetWorkAble(){

    }

    protected void onNetWorkUnable(){
        ToastUtils.show(this, getResources().getString(R.string.common_network_err), Toast.LENGTH_SHORT);
    }

    protected boolean isNeedRegsterNetWork(){
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isNeedRegsterNetWork()){


            if( android.os.Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.LOLLIPOP) {
                networkCallback = new ConnectivityManager.NetworkCallback() {
                    @Override
                    public void onAvailable(Network network) {
                        super.onAvailable(network);
                        AndroidSchedulers.mainThread().scheduleDirect(new Runnable() {
                            @Override
                            public void run() {
                                onNetWorkAble();
                            }
                        });

                    }

                    @Override
                    public void onLost(Network network) {
                        super.onLost(network);
                        AndroidSchedulers.mainThread().scheduleDirect(new Runnable() {
                            @Override
                            public void run() {
                                onNetWorkUnable();
                            }
                        });
                    }

                };
                NetworkRequest.Builder builder = new NetworkRequest.Builder();
                NetworkRequest request = builder.build();
                ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connMgr != null) {
                    connMgr.registerNetworkCallback(request, networkCallback);
                }
            }else{
                netwokReceive = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        boolean isAble = DeviceUtils.isNetworkAvailable(AppBaseActivity.this);
                        if(isAble){
                            onNetWorkAble();
                        }else{
                            onNetWorkUnable();
                        }
                    }
                };

                IntentFilter filter = new IntentFilter();
                filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(isNeedRegsterNetWork()){
            if(android.os.Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.LOLLIPOP && networkCallback != null){
                ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connMgr != null) {
                    connMgr.unregisterNetworkCallback(networkCallback);
                }
            }else if(netwokReceive != null) {
                unregisterReceiver(netwokReceive);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mUnBinder != null){
            mUnBinder.unbind();
        }

    }



    public boolean needButterKnife(){
        return false;
    }

    public int getLayoutId(){
        return 0;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        PLog.d("onNewIntent");
        super.onNewIntent(intent);
        List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
        if (fragmentList != null) {
            for (Fragment fragment : fragmentList) {
                if(fragment == null || !fragment.isAdded()){
                    continue;
                }
                if (fragment instanceof AppBaseFragment) {
                    ((AppBaseFragment) fragment).onNewIntent(intent);
                }
            }
        }
    }


}
